package com.controller;

import com.alibaba.fastjson.JSONObject;
import com.entity.Help;
import com.entity.UserInfo;
import com.entity.chat.*;
import com.service.ChatmsgService;
import com.service.FriendsService;
import com.service.HelpService;
import com.service.UserInfoService;
import com.util.StatusCode;
import com.vo.ResultVo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Controller
/* 校园互助模块控制层 */
public class HelpController {
	@Autowired
    private HelpService helpService;
		
	/**
     * 首页校园互助模控制
     * */
    @GetMapping("/user/newslist")
    public String userNews(){
        return "/common/listnews";
    }
    
    
    /* 校园互助模块详情页控制层 */
    @RequestMapping("/html/help-detail")
    public String help() {
    	 return "/common/help-detail";
    	}
      
    /**插入求助*/
    @ResponseBody
    @RequestMapping("/help/insert")
    public ResultVo InsertHelp (@RequestBody Help help,HttpSession session) {
    	help.setUser_id((String)session.getAttribute("userid"));
        Integer i = helpService.insertHelp(help);
        if (i == 1){
            return new ResultVo(true, StatusCode.OK,"设置成功");
        }
        return new ResultVo(true, StatusCode.ERROR,"设置失败");
    }
    /**查询求助记录 */
    @ResponseBody
    @RequestMapping("/help/look")
    public ResultVo LookHelp () {
    	List<Help> helps = helpService.lookHelp(new UserInfo());
        if (helps != null){
            return new ResultVo(true, StatusCode.OK, "设置成功" ,helps);
        }
        return new ResultVo(true, StatusCode.ERROR,"设置失败",helps);
    }
    /**查询一条求助记录 */
    @ResponseBody
    @RequestMapping("/help/look/{id}")
    public ResultVo lookIdHelp (@PathVariable("id") String  help_id) {
    	Help help = helpService.lookIdHelp(Integer.parseInt(help_id));
        if (help != null){
            return new ResultVo(true, StatusCode.OK, "设置成功" ,help);
        }
        return new ResultVo(true, StatusCode.ERROR,"设置失败",help);
    }
    /**修改一条具体的求助记录*/
    @ResponseBody
    @RequestMapping("/help/update")
    public ResultVo UpdateHelp (@RequestBody Help help) {
    	Integer i = helpService.updateHelp(help);
        if (i == 1){
            return new ResultVo(true, StatusCode.OK,"设置成功");
        }
        return new ResultVo(true, StatusCode.ERROR,"设置失败");
    }
    /**修改一条具体的求助记录*/
    @ResponseBody
    @RequestMapping("/help/updateflag")
    public ResultVo UpdateFlagHelp (@RequestBody Help help) {
    	Integer i = helpService.updateFlagHelp(help);
        if (i == 1){
            return new ResultVo(true, StatusCode.OK,"设置成功");
        }
        return new ResultVo(true, StatusCode.ERROR,"设置失败");
    }
    /**删除一条求助记录 */
    @ResponseBody
    @RequestMapping("/help/delete/{id}")
    public ResultVo DeleteHelp (@PathVariable("id") String  help_id) {
    	Integer i = helpService.deleteHelp(Integer.parseInt(help_id));
    	 if (i == 1){
             return new ResultVo(true, StatusCode.OK,"设置成功");
         }
         return new ResultVo(true, StatusCode.ERROR,"设置失败");
    }
    
    /**首页互助模块，查询互助的内容 */
    @ResponseBody
    @RequestMapping("/help/getdata")
    public ResultVo getdata (String type) {
    	System.err.println(type);
    	if (type.equals("默认")) {
    		List<Help> helps = helpService.lookHelp(new UserInfo());
        	ResultVo rv=new ResultVo();
        	rv.setMessage("查询成功");
        	rv.setData(helps);
    	    return rv;
		}else {
			List<Help> helps = helpService.lookTepyHelp(type);
	    	ResultVo rv=new ResultVo();
	    	rv.setMessage("查询成功");
	    	rv.setData(helps);
		    return rv;
		}
    	
    }
}
